import {
  Body,
  Controller,
  Get,
  Param,
  ParseIntPipe,
  Post,
  Put,
  Query,
  Req,
} from '@nestjs/common';
import { OrdersService } from './orders.service';
import { CreateOrderDto } from './dto/create';
import { RequiredAuth } from 'src/auth/decorators/auth.decorator';
import { User } from 'src/auth/decorators/user.decorator';
import { UserClaims } from 'src/shared/user';
import { $Enums, OrderStatus } from '@prisma/client';

@Controller('orders')
export class OrdersController {
  constructor(private readonly ordersService: OrdersService) {}

  @Post()
  @RequiredAuth()
  async create(@Body() body: CreateOrderDto, @User() { id }: UserClaims) {
    const count = await this.ordersService.create(id, body);

    return count;
  }

  @Get()
  @RequiredAuth()
  async getMyOrders(@User() { id }: UserClaims) {
    const orders = await this.ordersService.getOrderByUserId(id);

    return orders;
  }

  @Get('/all')
  @RequiredAuth()
  async getAllOrders(@Query('status') status?: $Enums.OrderStatus) {
    const orders = await this.ordersService.getAllOrders(status);

    return orders;
  }

  @Put('/:id')
  @RequiredAuth()
  async changeOrderStatus(
    @Param('id', new ParseIntPipe()) id: number,
    @Query('status') status: $Enums.OrderStatus,
  ) {
    const item = await this.ordersService.changeOrderStatus(id, status);

    return item;
  }

  @Get('/:id')
  @RequiredAuth()
  async getOrderItems(@Param('id', new ParseIntPipe()) id: number) {
    const items = await this.ordersService.getOrderItems(id);

    return items;
  }
}
